package com.ruoyi.project.wx.service.impl;


import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.bootdo.PageHelperUtils;
import com.ruoyi.common.utils.bootdo.R;
import com.ruoyi.common.utils.bootdo.UuidUtil;
import com.ruoyi.project.wx.dao.MemberCollectDao;
import com.ruoyi.project.wx.domain.MemberCollectDO;
import com.ruoyi.project.wx.service.MemberCollectService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class MemberCollectServiceImpl implements MemberCollectService {
	@Autowired
	private MemberCollectDao memberCollectDao;
	
	@Override
	public MemberCollectDO get(String id){
		return memberCollectDao.get(id);
	}
	
	@Override
	public List<MemberCollectDO> list(Map<String, Object> map){
		return memberCollectDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return memberCollectDao.count(map);
	}
	
	@Override
	public int save(MemberCollectDO memberCollect){
		return memberCollectDao.save(memberCollect);
	}
	
	@Override
	public int update(MemberCollectDO memberCollect){
		return memberCollectDao.update(memberCollect);
	}
	
	@Override
	public int remove(String id){
		return memberCollectDao.remove(id);
	}
	
	@Override
	public int batchRemove(String[] ids){
		return memberCollectDao.batchRemove(ids);
	}

    @Override
    public List<MemberCollectDO> likeList(Map<String, Object> map){
        return memberCollectDao.likeList(map);
    }
    @Override
    public int likeListCount(Map<String, Object> map){
        return memberCollectDao.likeListCount(map);
    }

    @Override
    public R saveCollect(Map<String, Object> params) {
		try {
			params.put("delFlag","0");
			List<MemberCollectDO> list = memberCollectDao.list(params);
			if(list.size()>0){
				memberCollectDao.remove(list.get(0).getId());
				return R.ok(0,"取消成功");
			}else{
				MemberCollectDO memberCollectDO = new MemberCollectDO();
				BeanUtils.populate(memberCollectDO,params);
				memberCollectDO.setCaAt(new Date());
				memberCollectDO.setDelFlag(0);
				memberCollectDO.setUpAt(new Date());
				memberCollectDO.setId(UuidUtil.get32UUID());
				memberCollectDao.save(memberCollectDO);
				return R.ok(0,"收藏成功");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("收藏失败");
		}

	}

	@Override
	public R getCollectList(Map<String, Object> params) {
		if(params.get("status").toString().equals("0")){
			//收藏得商品
			PageHelperUtils.page(params);
			List<Map<String,Object>> collectList = memberCollectDao.getGoodsCollect(params);
			PageInfo<Map<String,Object>> pageInfo = new PageInfo<>(collectList);
			return R.ok().put("data",pageInfo);
		}else {
			//收藏得店铺
			PageHelperUtils.page(params);
			List<Map<String,Object>> collectList = memberCollectDao.getShopCollect(params);
			PageInfo<Map<String,Object>> pageInfo = new PageInfo<>(collectList);
			return R.ok().put("data",pageInfo);
		}

	}

}
